﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.Silverlight.Library.RichContentService;
using FRL.DebtChased.Types;

namespace FRL.DebtChased.Silverlight.Library
{

	/// <summary>
	/// Declaration of BankAccountCtrl class
	/// Control for editing bank account details
	/// </summary>
	public partial class BankAccountCtrl : UserControl
	{

		/////////////////////////////////////////////////////////////
		// Delegates and events

		public event EventHandler<ChildCtrlContentChangeEventArg> ContentChanged;

		public event EventHandler DeleteButtonClicked;

		/////////////////////////////////////////////////////////////
		// Attributes

		private int m_iBankAccountId = 0;
		public int BankAccountId
		{
			get { return m_iBankAccountId; }
			set { m_iBankAccountId = value; }
		}

		/////////////////////////////////////////////////////////////
		// Construction

		public BankAccountCtrl()
		{

			// Load xaml
			InitializeComponent();

			// Set controls
			Money.LoadCurrencyControl(cboCurrency, true);
			cboCurrency.SelectedIndex = 0;

			// Load the bank names
			drpBank.StringList = CachedDataBase.SingletonBase.BankNames;

			// Setup the address control
//addressCtrl.PreDisplay();

		}

		private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
		{

			// Setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void butDelete_Click(object sender, RoutedEventArgs e)
		{

			// Option for user
			if (!MessageBoxes.PopupOption("Delete this bank account?"))
				return;

			// Inform the owner tab-control
			if (DeleteButtonClicked != null)
				DeleteButtonClicked(this, e);

		}

		/////////////////////////////////////////////////////////////
		// Event Handlers

		private void drpBank_TextChanged(object sender, RoutedEventArgs e)
		{

			// Fire event
			_fireContentChangedEvent(true);

		}

		private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
		{

			// Fire event
			_fireContentChangedEvent(
				(sender == txtAccountName)
				||
				(sender == txtAccNumber)
			);

		}

		private void ContactAddressCtrl_ContentChanged(object sender, ChildCtrlContentChangeEventArg e)
		{

			// Fire event
			_fireContentChangedEvent(false);

		}

		private void cboCurrency_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{

			// Fire event
			_fireContentChangedEvent(true);

		}

		public void OnTabIndexChange()
		{

			// Setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Operations

		public string ComposeTabHeading()
		{

			// Declare return variable
			string strFtmp = null;

			// Find a suitable address heading
			if (txtAccNumber.Text.Length > 0)
				strFtmp = txtAccNumber.Text;
			else if (txtAccountName.Text.Length > 0)
				strFtmp = txtAccountName.Text;
			else if (drpBank.Text.Length > 0)
				strFtmp = drpBank.Text;
			if (strFtmp == null)
				strFtmp = "[bank account]";
			strFtmp = strFtmp.DotDotDot(10);

			// Add currency
			string strCurrency = Money.SafeGetCurrencyCode(cboCurrency);
			if (strCurrency != null)
			{
				Money.eCurrency currency = Money.ParseCurrencyCode(strCurrency);
				strFtmp += string.Format(" ({0})", Money.CurrencyToSymbol(currency));
			}

			// Return variable
			return strFtmp;

		}

		private void _fireContentChangedEvent(bool bUpdateHeading)
		{

			// Fire event
			if (ContentChanged != null)
				ContentChanged(this, new ChildCtrlContentChangeEventArg(bUpdateHeading));

		}

		public void Set(BankAccountTransport bankAccountTransport)
		{

			// Set controls
			this.BankAccountId = bankAccountTransport.BankAccountId;
			drpBank.Text = bankAccountTransport.BankName;
			txtAccountName.Text = bankAccountTransport.AccountName ?? string.Empty;
			txtSortCode.Text = bankAccountTransport.SortCode ?? string.Empty;
			txtAccNumber.Text = bankAccountTransport.AccountNo ?? string.Empty;
			txtIBANNo.Text = bankAccountTransport.IBANNo ?? string.Empty;
			txtSwiftCode.Text = bankAccountTransport.SwiftCode ?? string.Empty;
			ComboBoxObject.SelectByTagString(cboCurrency, bankAccountTransport.CurrencyCode);
//addressCtrl.Set(bankAccountTransport.Address);
			butDelete.IsEnabled = !bankAccountTransport.InUse;

			// Setup controls
			_setupControls();

		}

		public void Get(BankAccountTransport bankAccountTransport, List<string> Errors)
		{

			// Populate the bank account details object with details from the control
			// and find errors in the user input

			// Get the currency code
			string strCurrencyCode = Money.SafeGetCurrencyCode(cboCurrency);

			// Check controls
			if (drpBank.Text.Length < 4)
				Errors.Add("Bank account bank name must have 4 characters.");
			if (txtAccountName.Text.Length < 4)
				Errors.Add("Bank account name must have 4 characters.");
			if (txtAccNumber.Text.Length < 8)
				Errors.Add("Bank account number must have 8 characters.");
			if (txtSortCode.Text.Length < 6)
				Errors.Add("Bank account sort code must have 6 characters.");
			if (strCurrencyCode == null)
				Errors.Add("Bank account currency must be set.");

			// Get values from controls
			bankAccountTransport.BankAccountId = this.BankAccountId;
			bankAccountTransport.BankName = drpBank.Text;
			bankAccountTransport.AccountName = txtAccountName.Text;
			bankAccountTransport.SortCode = txtSortCode.Text;
			bankAccountTransport.AccountNo = txtAccNumber.Text;
			bankAccountTransport.IBANNo = txtIBANNo.Text;
			bankAccountTransport.SwiftCode = txtSwiftCode.Text;
			bankAccountTransport.CurrencyCode = strCurrencyCode;

			// Get the address
			List<string> AddressErrors = new List<string>();
//bankAccountTransport.Address = addressCtrl.Get(AddressErrors);
			if (AddressErrors.Count > 0)
				Errors.Add("Bank account must have an address.");

		}

		private void _setupControls()
		{

			// Get the parent tab control
			TabItem parentTabItem = this.FindParent(typeof(TabItem)) as TabItem;

			// Bail out in design mode
			if (parentTabItem == null)
				return;

			// Find tab item index
			TabControl parentTabCtrl = parentTabItem.FindParent(typeof(TabControl)) as TabControl;
			int iIndex = parentTabCtrl.Items.IndexOf(parentTabItem);
			if (
				(m_iBankAccountId == 0)
				&&
				(iIndex != (parentTabCtrl.Items.Count - 1))
			)
				butDelete.Visibility = System.Windows.Visibility.Visible;
			else
				butDelete.Visibility = System.Windows.Visibility.Collapsed;

		}

	}

}
